<template>
  <!-- container布局 -->
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/me.jpg" alt="头像" />
        <span>个人运动平台</span>
      </div>
      <el-button type="info" @click="logout">安全退出</el-button>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu background-color="#545c64" text-color="#fff" active-text-color="#409eff" 
        unique-opened :collapse="isCollapse" :collapse-transition="false"
        :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="menu.id+''" v-for="menu in menuList" :key="menu.id">
            <template slot="title">
              <i :class="iconList[menu.id]"></i>&nbsp;
              <span>{{menu.title}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="item.path" v-for="item in menu.sList" :key="item.id" @click="saveNavState(item.path)">
              <template slot="title">
                <i :class="iconList[item.id]"></i>&nbsp;
                <span>{{item.title}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
      return {
          menuList: [],
          isCollapse: false,
          iconList: {
            '100': 'iconfont icon-guanliyuan',
            '200': 'iconfont icon-yundong',
            '1': 'iconfont icon-denglu',
            '2': 'iconfont icon-mima',
            '3': 'iconfont icon-yundong',
            '4': 'iconfont icon-shangpin',
            '5': 'iconfont icon-shu',
            '6': 'iconfont icon-qialuli',
            '7': 'iconfont icon-shiwu-',
            '8': 'iconfont icon-denglu'
          },
          activePath: '/welcome'
      }
  },
  created() {
      // 查询menuList
      this.getMenuList();
      this.activePath = window.sessionStorage.getItem('activePath');
  },
  methods: {
    logout() {
      // 清除缓存
      window.sessionStorage.clear();
      this.$router.push({
        path: "/login"
      });
    },
    // 获取导航菜单
   async getMenuList() {
        const {data:res} = await this.$http.get("/menu/getMenus");
        if(res.code != 200) return this.$message.error('获取列表失败！');
        this.menuList = res.menus;
    },
    // 控制伸缩
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    // 保存路径
    saveNavState(path) {
      window.sessionStorage.setItem('activePath',path);
      this.activePath = path;
      this.$router.push({
        path: path
      })
    }
  }
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0%;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}

img {
  width: 55px;
  height: 55px;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>